﻿using System.Xml.Serialization;

namespace Development.Network.Msg
{
    ///协议模型
    /// 消息包长度  4 个字节 类型 int
    /// 消息MsgId  4 个字节  类型 int
    /// UserId 指用来保存session的用户id 4 个字节
    /// _Status 预留字段 4个字节
    public class MsgBase : IMsg
    {
        [XmlIgnore]//json 忽略
        public int _Lenth;           //消息长度
        [XmlIgnore]//json 忽略
        public int _MsgId;           //消息id
        [XmlIgnore]
        public int _UserId;          //用户id
        [XmlIgnore]
        public int _Status;          //预留用作错误代码

        public MsgBase()
        {
            WriteMsgId();
        }

        protected virtual void WriteMsgId()
        {
            _MsgId = 0;
        }

        public virtual byte[] Pack()
        {
            MsgSn sn = new MsgSn();
            //为长度占位
            sn.Write(0);
            sn.Write(_MsgId);
            sn.Write(_UserId);
            sn.Write(_Status);
            _Lenth = sn.CalcLen();
            return sn.GetMsg();
        }
        public virtual void Unpack(MsgPacket packet)
        {
            _Lenth = packet.Lenth;
            _MsgId = packet.MsgId;
            _UserId = packet.UserId;
            _Status = packet.Status;
        }
    }
}
